package com.suke.czx.modules.wxadmin.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.suke.czx.common.utils.DateUtils;
import com.suke.czx.common.utils.RedisUtils;
import com.suke.czx.modules.wx.config.WxPayProperties;
import com.suke.czx.modules.wx.vo.UserReadDays;
import com.suke.czx.modules.wxadmin.dao.DkDomianDao;
import com.suke.czx.modules.wxadmin.dao.DkReadPlanDao;
import com.suke.czx.modules.wxadmin.dao.DkResourceDao;
import com.suke.czx.modules.wxadmin.dao.DkUserDao;
import com.suke.czx.modules.wxadmin.entity.DkDomianEntity;
import com.suke.czx.modules.wxadmin.entity.DkReadPlanEntity;
import com.suke.czx.modules.wxadmin.entity.DkUserEntity;
import com.suke.czx.modules.wxadmin.service.DkReadPlanService;

@Service("dkReadPlanService")
public class DkReadPlanServiceImpl implements DkReadPlanService {
	@Autowired
	private DkReadPlanDao dkReadPlanDao;
	@Autowired
	private DkUserDao dkUserDao;
	@Autowired
	private DkDomianDao dkDomianDao;
	@Autowired
	private DkResourceDao dkResourceDao;
	@Autowired
	private RedisUtils redisUtils;
	@Autowired 
	private WxPayProperties wxPayProperties;
	

	@Override
	public DkReadPlanEntity queryObject(String id) {
		return dkReadPlanDao.queryObject(id);
	}

	@Override
	public List<DkReadPlanEntity> queryList(Map<String, Object> map) {
		return dkReadPlanDao.queryList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return dkReadPlanDao.queryTotal(map);
	}

	@Override
	public void save(DkReadPlanEntity dkReadPlan) {
		dkReadPlanDao.save(dkReadPlan);
	}

	@Override
	public void update(DkReadPlanEntity dkReadPlan) {
		dkReadPlanDao.update(dkReadPlan);
	}

	@Override
	public void delete(String id) {
		dkReadPlanDao.delete(id);
	}

	@Override
	public void deleteBatch(String[] ids) {
		dkReadPlanDao.deleteBatch(ids);
	}

	@Override
	public int batchInsertUserReadPlan(List<DkReadPlanEntity> userReadPlanList) {
		return dkReadPlanDao.batchInsertUserReadPlan(userReadPlanList);
	}

	@Override
	public List<DkReadPlanEntity> queryUserReadPlanById(String userId) {
		return dkReadPlanDao.queryUserReadPlanById(userId);
	}

	@Override
	public DkReadPlanEntity queryUserReadPlanByIdAndDate(Map<String, String> parms) {
		return dkReadPlanDao.queryUserReadPlanByIdAndDate(parms);
	}
	
	
	

	@Override
	public UserReadDays getUserReadDays(String openid) {

		// 构建打卡页面用户分享数据
		List<DkReadPlanEntity> list = dkReadPlanDao.queryUserReadPlanById(openid);
		int readDays = 0;
		int tmp_continuousReadDays = 0;
		int continuousReadDays = 0;
		String tomorrowDate = DateUtils.getDate(new Date(), 1);
		String taodyDate = DateUtils.getDate(new Date(), 0);
		String dates = "";
		for (DkReadPlanEntity urp : list) {
			dates = urp.getReadDate();
			if (tomorrowDate.equals(dates)) {
				break;
			}
			if (urp.getStatus() == 1) {
				readDays++;
				continuousReadDays++;
			} else if (urp.getStatus() == 2) {
				readDays++;
				continuousReadDays++;
			}else if (urp.getStatus() == 3) {
				readDays++;
				continuousReadDays++;
			} else {

				if (!taodyDate.equals(dates)) {
					if (continuousReadDays > tmp_continuousReadDays) {
						tmp_continuousReadDays = continuousReadDays;
					}
					continuousReadDays = 0;
				}
			}
		}

		if (continuousReadDays > tmp_continuousReadDays) {
			tmp_continuousReadDays = continuousReadDays;
		}

		// 获取对应的用户信息
		DkUserEntity dkUser = dkUserDao.queryObject(openid);
		UserReadDays userReadDays = new UserReadDays();
		userReadDays.setTmp_continuousReadDays(tmp_continuousReadDays);
		userReadDays.setReadDays(readDays);
		userReadDays.setContinuousReadDays(continuousReadDays);
		userReadDays.setOpenid(openid);
		userReadDays.setNickname(dkUser.getNickname());
		userReadDays.setHeadimgurl(dkUser.getHeadimg());
		return userReadDays;
	}
	
	public int getUserReadDaysLianXu(String openid) {

		// 构建打卡页面用户分享数据
		List<DkReadPlanEntity> list = dkReadPlanDao.queryUserReadPlanById(openid);
		int readDays = 0;
		for (DkReadPlanEntity urp : list) {
			if (urp.getStatus() == 1) {
				readDays++;
			} else if (urp.getStatus() == 2) {
				readDays++;
			} else if (urp.getStatus() == 3) {
				readDays++;
			}else {
				break;
			}
		}
		
		return readDays;
	}
	
	

	@Override
	public int updateSendStateBatch(Object[] id) {
		return dkReadPlanDao.updateSendStateBatch(id);
	}

	@Override
	public JSONObject planIndex(String openid) {
		List<DkReadPlanEntity> list = dkReadPlanDao.queryUserReadPlanById(openid);
		String todayShareTitle = "";
		
		StringBuffer read = new StringBuffer();
    	StringBuffer unread = new StringBuffer();
    	StringBuffer fillRead = new StringBuffer();
    	int readCount = 0;
    	int unreadCount = 0;
    	String today = DateUtils.getDate(new Date(),0);
    	String tomorrowDate = DateUtils.getDate(new Date(),1);
    	for(DkReadPlanEntity urp : list) {
    		if (tomorrowDate.equals(urp.getReadDate())) {
    			break;
    		}
    		if(today.equals(urp.getReadDate())){
    			todayShareTitle = dkResourceDao.queryObject(urp.getResourceId()).getShareTitle();
    		}
    		if(urp.getStatus() == 0) {
    			unreadCount++;
				unread.append(urp.getReadDate()).append(",");
    		} else {
    			readCount++;
    			if(urp.getStatus() == 3){
    				fillRead.append(urp.getReadDate()).append(",");
    			}else{
    				read.append(urp.getReadDate()).append(",");
    			}
    		}
    	}
    	//System.out.println(list.get(0).getReadDate());
    	if (read.length() > 0) read.deleteCharAt(read.length()-1);
    	if (unread.length() > 0) unread.deleteCharAt(unread.length()-1);
    	if (fillRead.length() > 0) fillRead.deleteCharAt(fillRead.length()-1);
    	
    	String url=redisUtils.getObject("memdkurldd"+wxPayProperties.getAppId());
    	
    	if(null==url){
    		Map<String, Object> params = new HashMap<String, Object>();
    		params.put("page", 1);
    		params.put("limit", 1000);
    		List<DkDomianEntity> domains  = dkDomianDao.queryList(params);
        	if(null!=domains&&domains.size()>0){
        		String domain = domains.get(0).getDomainName().trim();
            	url = "http://"+domain;
        	}
        	redisUtils.setObject("memdkurldd"+wxPayProperties.getAppId(), url, 7200);
    	}
    	
    	String str = "{\"read\":\""+ read.toString() 
    	+ "\",\"unread\":\"" + unread.toString() 
    	+ "\",\"fillRead\":\"" + fillRead.toString() 
    	+ "\",\"todayShareTitle\":\"" + todayShareTitle
    	+ "\",\"unreadDays\":" + unreadCount 
    	+ ",\"dayIndex\":" + readCount 
    	+ ",\"domain\":\"" + url 
    	+ "\",\"readClass\":23,\"today\":\"" + today + "\" }";
		
    	JSONObject object = JSONObject.parseObject(str);
		
		return object;
	}
	
	
	public List<DkDomianEntity> getDomianList(){
//		List<DkDomianEntity> domains = redisUtils.getObject("memdomains");
//		if(domains!=null){
//			return domains;
//		}
//		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", 1);
		params.put("limit", 1000);
		List<DkDomianEntity> domains  = dkDomianDao.queryList(params);
		//redisUtils.setObject("memdomains", domains, 10000);
		return domains;
	}
	

}
